﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EWC.HIS.Dapper.Data;
using Microsoft.Extensions.Configuration;
using EWC.Core.Util.Settings;
using EWC.HIS.Domain.Data.Repositories;
using EWC.HIS.Domain.Data.Enums;

namespace EWC.HIS.Dapper
{
    public class DbContextFactory
    {
        private const string HIS_CONNECTION_STRING_KEY = "ConnectionStrings:HIS";
        private const string EMGDB_CONNECTION_STRING_KEY = "ConnectionStrings:EmgDB";

        // 添加一个映射表，将连接字符串键值与枚举类型对应
        private readonly static Dictionary<EDbConnStr, string> ConnectionStringMap = new Dictionary<EDbConnStr, string>
                        {
                            { EDbConnStr.HIS, HIS_CONNECTION_STRING_KEY },
                            { EDbConnStr.EmgDB, EMGDB_CONNECTION_STRING_KEY },
                        };

        private static async Task<IDbConnection> CreateConnectionAsync(EDbConnStr eDbConnStr = EDbConnStr.HIS)
        {
            var connectionString = AppSettings.GetValue(ConnectionStringMap[eDbConnStr]);

            var conn = new SqlConnection(connectionString);
            await conn.OpenAsync();
            return conn;
        }

        //public static async Task<IDbContext> CreateSessionAsync(EDbConnStr eDbConnStr)
        //{
        //    IDbConnection conn = await CreateConnectionAsync(eDbConnStr);
        //    return new DbContext(conn);
        //}
    }
}
